package models

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/pkg/errors"
	"time"
)

type RetailsLog struct {
	Id        int64
	Retails   *Retails `orm:"rel(fk);null;on_delete(do_nothing)" json:"-"`
	Content   string
	CreatedAt int64
	CreatedBy string
}

func (a *RetailsLog) TableName() string {
	return RetailsLogTableName()
}

func RetailsLogTableName() string {
	return TableName("sa_retails_log")
}

//查询列表数据
func RetailsLogList(retails_id int64) []*RetailsLog {
	query := orm.NewOrm().QueryTable(RetailsLogTableName())
	data := make([]*RetailsLog, 0)
	query = query.Filter("retails_id", retails_id)
	query.OrderBy("id").All(&data)
	return data
}

//写入日志
func WriteRetailsLog(o orm.TxOrmer, retails_id int64, content string, u *BackendUser) error {
	if retails_id == 0 {
		return errors.New("参数错误")
	}
	log := new(RetailsLog)
	log.Retails = &Retails{Id: retails_id}
	log.Content = content
	log.CreatedAt = time.Now().Unix()
	log.CreatedBy = u.RealName
	if _, err := o.Insert(log); err != nil {
		return err
	}
	return nil
}
